﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_AddWork : System.Web.UI.Page
{
    LocalFunctions FUNC;
    DataTable dtTags;
    int iWorkID;

    protected void Page_Load(object sender, EventArgs e)
    {
        FUNC = ((Admin)this.Master).FUNC;
        GetParameters();
        // Get data from DB about page
        if (!IsPostBack)
        {
            GetData();
        }
        if (dtTags == null)
            if (ViewState["tags"] != null)
                dtTags = (DataTable)ViewState["tags"];
            else
            {
                dtTags = new DataTable();
                dtTags.Columns.Add("KeyID");
                dtTags.Columns.Add("KeyName");
            }
    }

    private void GetParameters()
    {
        iWorkID = Convert.ToInt32(Request.QueryString["ID"]);
    }

    private void GetData()
    {
        DataTable DT = Dal.ExecuteDBsp("Admin_GetTags");
        // If not null - fill fields
        if (DT != null)
            if (DT.Rows.Count > 0)
            {
                ddTags.DataSource = DT;
                ddTags.DataBind();
            }
        if (iWorkID > 0)
        {
            GetWorkByID();
        }
        else
        {
            pnlImages.Visible = false;
        }

    }

    private void GetWorkByID()
    {
        Dal.arParams.Clear();
        Dal.arParams.Add(iWorkID);
        DataSet DS=Dal.ExecuteDBspDS("Admin_GetWorkByID", Dal.arParams);

        if(DS!=null)
            if (DS.Tables[0].Rows.Count > 0)
            {
                DataRow dRow = DS.Tables[0].Rows[0];
                txtTitle.Text = dRow["Title"].ToString();
                txtSubTitle.Text=dRow["SubTitle"].ToString();
                txtContent.Text=dRow["Content"].ToString();
                txtTechnologies.Text=dRow["Details"].ToString();
                hfThumb.Value = dRow["thumbnail"].ToString();
                txtLinkName.Text=dRow["LinkName"].ToString();
                txtLinkUrl.Text = dRow["Link"].ToString();
            }
        if (DS.Tables.Count > 1)
        {
            if (DS.Tables[1].Rows.Count > 0)
            {
                dtTags = DS.Tables[1];
                ViewState["tags"] = dtTags;

                rptTags.DataSource = dtTags;
                rptTags.DataBind();
            }
        }

        if (DS.Tables.Count > 2)
        {
            if (DS.Tables[2].Rows.Count > 0)
            {
                foreach (DataRow Row in DS.Tables[2].Rows)
                {
                    switch (Convert.ToInt32(Row["placeID"]))
                    {
                        case 1:
                            img1.ImageUrl = "~/content/galery/" + Row["preview"].ToString();
                            
                            break;
                        case 2:
                            img2.ImageUrl = "~/content/galery/" + Row["preview"].ToString();
                            
                            break;
                        case 3:
                            img3.ImageUrl = "~/content/galery/" + Row["preview"].ToString();
                            
                            break;
                    }
                }
               
            }
            
        }

        img1.NavigateUrl = string.Format("ImageCrop.aspx?work={0}&place={1}", iWorkID, 1);
        img2.NavigateUrl = string.Format("ImageCrop.aspx?work={0}&place={1}", iWorkID, 2);
        img3.NavigateUrl = string.Format("ImageCrop.aspx?work={0}&place={1}", iWorkID, 3);
    }

    // Add new tag
    protected void btnAddTag_Click(object sender, EventArgs e)
    {
        DataRow dr = dtTags.NewRow();
        dr["KeyID"] = ddTags.SelectedItem.Value;
        dr["KeyName"] = ddTags.SelectedItem.Text;
        dtTags.Rows.Add(dr);

        ViewState["tags"] = dtTags;

        rptTags.DataSource = dtTags;
        rptTags.DataBind();
    }

    protected void imgDelete_Click(object sender, ImageClickEventArgs e)
    {
        int iTagID;
        Int32.TryParse(((ImageButton)sender).CommandArgument.ToString(), out iTagID);
        DataRow dr = dtTags.Select("keyid=" + iTagID)[0];
        dtTags.Rows.Remove(dr);
        ViewState["tags"] = dtTags;
        rptTags.DataSource = dtTags;
        rptTags.DataBind();
        if (iWorkID > 0)
        {
            Dal.arParams.Clear();
            Dal.arParams.Add(iWorkID);
            Dal.arParams.Add(iTagID);
            Dal.ExecuteDBsp("Admin_DeleteTagToWork", Dal.arParams);
        }
    }

    protected void btnArticleSave_Click(object sender, EventArgs e)
    {
        String sThumbImg = hfThumb.Value;
        if (uplThumb.HasFile)
        {
            string sExt = System.IO.Path.GetExtension(uplThumb.PostedFile.FileName);
            sThumbImg = "thumb_" + Helper.GenerateUniqueFileName() + sExt;
            uplThumb.SaveAs(LocalFunctions.sMappedPath + "\\" + sThumbImg);
        }

        Dal.arParams.Clear();
        Dal.arParams.Add(iWorkID);
        Dal.arParams.Add(txtTitle.Text);
        Dal.arParams.Add(txtSubTitle.Text);
        Dal.arParams.Add(txtContent.Text);
        Dal.arParams.Add(txtTechnologies.Text);

        Dal.arParams.Add(txtLinkName.Text);
        Dal.arParams.Add(txtLinkUrl.Text);

        Dal.arParams.Add(sThumbImg); // Thumbnails
        Dal.arParams.Add(FUNC.LangID); // Lang

        iWorkID = Convert.ToInt32(Dal.ExecuteDBspOneValue("Admin_UpdateWork", Dal.arParams, true));

        // Add tags to article
        foreach (DataRow dr in dtTags.Rows)
        {
            Dal.arParams.Clear();
            Dal.arParams.Add(iWorkID);
            Dal.arParams.Add(Convert.ToInt32(dr["KeyID"]));
            Dal.ExecuteDBsp("Admin_InsertTagToArticle", Dal.arParams);
        }

        img1.NavigateUrl = string.Format("ImageCrop.aspx?work={0}&place={1}", iWorkID, 1);
        img2.NavigateUrl = string.Format("ImageCrop.aspx?work={0}&place={1}", iWorkID, 2);
        img3.NavigateUrl = string.Format("ImageCrop.aspx?work={0}&place={1}", iWorkID, 3);
    }
}